from django.shortcuts import render
# Create your views here.
from django.views import View
from areas.models import Area
from django import http

import logging
# 创建日志输出器
from meiduo_mall.utils.response_code import RETCODE
from django.core.cache import cache # 导入缓存模块
logger=logging.getLogger("django")
class AreasView(View):
    """省市区三级联动"""
    def get(self,request):
        # 判断当前要查询的是省份数据还是市区数据
        area_id = request.GET.get("area_id")
        if not area_id:
            # 获取并判断是否有缓存
            province_list = cache.get("province_list")
            if not province_list:
                # 查询省份数据
                try:
                    # Area.objects.filter(属性名__条件表达式=值)
                    province_model_list = Area.objects.filter(parent__isnull=True)

                    # 将模型列表转化为字典列表
                    province_list = []
                    for province_model in province_model_list:
                        province_dict = {
                            "id":province_model.id,
                            "name":province_model.name
                        }
                        province_list.append(province_dict)
                    # 缓存省份字典列表数据:默认存储在名为"default"的配置中
                    cache.set("province_list", province_list, 3600)

                except Exception as e:
                    logger.error(e)
                    return http.JsonResponse({"code":RETCODE.DBERR,"errmsg":"查询省份数据错误"})
            return http.JsonResponse({"code": RETCODE.OK, "errmsg": "OK", "province_list": province_list},
                                         json_dumps_params={"ensure_ascii": False})
        else:
            """
                {
                  "code":"0",
                  "errmsg":"OK",
                  "sub_data":{
                      "id":130000,
                      "name":"河北省",
                      "subs":[
                          {
                              "id":130100,
                              "name":"石家庄市"
                          },
                          ......
                      ]
                  }
                }
            """
            # 判断是否有缓存
            sub_data = cache.get("sub_area_"+area_id)
            if not sub_data:
                # 查询城市获取区县数据
                parent_model=Area.objects.get(id=area_id)
                # 默认由一查询多的方式
                # sub_model_list = parent_model.area_set.all()
                # 但是当前模型类设置了related_name = 'subs' 属性，
                # 所以可以通过
                sub_model_list = parent_model.subs.all()
                # 将子级模型列表转化成字典列表
                subs = []
                for sub_model in sub_model_list:
                    sub_dict = {
                        "id":sub_model.id,
                        "name":sub_model.name
                    }
                    subs.append(sub_dict)
                # 构造子级JSON数据
                sub_data = {
                    "id":parent_model.id,
                    "name":parent_model.name,
                    "subs":subs
                }
                # 缓存城市或者区县
                cache.set("sub_area_"+area_id,sub_data,3600)
            # 响应城市或者区县数据
            return http.JsonResponse({"code":RETCODE.OK,"errmsg":"OK","sub_data":sub_data},
                                     json_dumps_params={"ensure_ascii":False})


